package com.nanfang51g3.ckz.ui.money.list;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.domain.MoneyDetailBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class MoneyDetailPresenter implements MoneyDetailContract.MoneyDetailActionsListener {

    String message;
    private final RestApi _restApi;
    private final MoneyDetailContract.View mView;

    @Inject
    public MoneyDetailPresenter(RestApi restApi, MoneyDetailContract.View mView) {
        KLog.i("TAG", "进入了...MoneyDetailPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see MoneyDetailPresenterModule#MoneyDetailPresenterModule(MoneyDetailContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }

    @Override
    public void requestMoneyDetail(int page, int limit, final int type) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        treeMap.put("userid", userid);
        treeMap.put("page", page + "");
        treeMap.put("limit", limit + "");
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", sign + "-" + treeMap.toString());
        Observable observable = _restApi.deallist(userid, page + "", limit + "", sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new MoneyHttpResultFunc<List<MoneyDetailBean>>())
                .subscribe(new SubscriberInterface<List<MoneyDetailBean>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        KLog.i("Evd", "onStart ");
                        mView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted ");
                        /**
                         * @see MoneyDetailActivity#dismissProgress()
                         */
                        mView.dismissProgress();
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "Throwable " + msg);
                        /**
                         * @see MoneyDetailActivity#showMessage(String)
                         */
                        mView.showMessage(msg);
                        /**
                         * @see MoneyDetailActivity#dismissProgress()
                         */
                        mView.dismissProgress();
                    }

                    @Override
                    public void onNext(List<MoneyDetailBean> o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        /**
                         * @see MoneyDetailActivity#showMessage(String)
                         */
                        List<MoneyDetailBean> mbs = new ArrayList<>();
                        mbs.addAll(o);
                        o.clear();
                        //处理数据
                        for (MoneyDetailBean re : mbs) {
                            if (re.getPay_type().equals(type + "")) {
                                o.add(re);
                            }
                        }
                        if (o.size() > 0) {
                            mbs.clear();
                            mView.showSuccess(o);
                        } else {
                            mView.showMessage("没有更多了");
                        }
                    }
                });

    }

    class MoneyHttpResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0-" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            if (

                    httpResult.getCode() == 3 ||
                    httpResult.getCode() == 4 ||
                    httpResult.getCode() == 5 ||
                    httpResult.getCode() == 6 ||
                    httpResult.getCode() == 7 ||
                    httpResult.getCode() == 8 ||
                    httpResult.getCode() == 9 ||
                    httpResult.getCode() == 10) {
                throw new Api2Exception(httpResult.getInfo());
            }
            if(httpResult.getCode() == 2){
                throw new Api2Exception("没有更多了");
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }
}
